package com.lishe.lishe.lishe.module.mall.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.base.common.base.BaseFragment;
import com.lishe.lishe.lishe.bean.SpaceItemDecoration;
import com.lishe.lishe.lishe.module.mall.adapter.Classification1Adapter;
import com.lishe.lishe.lishe.module.mall.adapter.Classification2Adapter;
import com.lishe.lishe.lishe.module.mall.bean.category.CategoryFirst;
import com.lishe.lishe.lishe.module.mall.fragment.IndexFragment;
import com.lishe.lishe.lishe.module.mall.presenter.ClassificationPresenter;
import com.lishe.lishe.lishe.utils.Utils;
import com.lishe.lishe.retrofit.DataResponseListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yang on 2017/11/6.
 */

public class ClassifyFragment extends BaseFragment implements View.OnClickListener {

    private LinearLayout mLoading2;
    private RecyclerView mBigClassRV, mSmallClassRV;
    private RelativeLayout mEmpty, mError;
    private View mErrorLayout;
    private List<CategoryFirst> mList1;
    private List<CategoryFirst> mList2;
    private Classification1Adapter mAdapter1;
    private Classification2Adapter mAdapter2;
    private int selectPosition = 0;
    private View rootView;// cache fragment view
    private View mContent;

    private volatile static ClassifyFragment singleton = null;

    public static ClassifyFragment getInstance()   {
        if (singleton == null)  {
            synchronized (ClassifyFragment.class) {
                if (singleton == null)  {
                    singleton= new ClassifyFragment();
                }
            }
        }
        return singleton;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.activity_classification, null);
            initView(rootView);
            initListener();
            setAdapter();
            getCategoryList1();
        }
        return rootView;
    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);
        if (getView() != null) {
            this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
        }
    }

    private void initView(View v) {
        mContent = v.findViewById(R.id.content);
        mBigClassRV = v.findViewById(R.id.bigclass_recycler);
        mSmallClassRV = v.findViewById(R.id.smallclass_recycler);
        mLoading2 = v.findViewById(R.id.loading2);
        mEmpty = v.findViewById(R.id.empty);
        mError = v.findViewById(R.id.error);
        mErrorLayout = v.findViewById(R.id.error_layout);
        v.findViewById(R.id.error_retry).setOnClickListener(this);
        mBigClassRV.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mSmallClassRV.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBigClassRV.addItemDecoration(new SpaceItemDecoration(0, 0, 0, Utils.dip2px(getActivity(), 1)));
    }

    private void initListener() {
        mErrorLayout.setOnClickListener(this);
        mError.setOnClickListener(this);
    }

    private void setAdapter() {
        mList1 = new ArrayList<>();
        mList2 = new ArrayList<>();
        mAdapter1 = new Classification1Adapter(getActivity(), mList1);
        mAdapter2 = new Classification2Adapter(getActivity(), mList2);
        mBigClassRV.setAdapter(mAdapter1);
        mSmallClassRV.setAdapter(mAdapter2);

        mAdapter1.setOnItemClickListener(new RecyclerviewBaseAdapter.ItemListener.ItemClickListener() {
            @Override
            public void OnClickListener(RecyclerView.Adapter adapter, View view, int position) {
                if (selectPosition != position) {
                    mAdapter1.setSelectPosition(position);
                    getCategoryList2(mList1.get(position).getCate_id());
                    selectPosition = position;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.error_retry:
                getCategoryList1();
                break;
            case R.id.error:
                getCategoryList2(mList1.get(selectPosition).getCate_id());
                break;
        }
    }

    /**
     * 获取首页一级分类列表
     */
    public void getCategoryList1() {

        if (mErrorLayout.getVisibility() == View.VISIBLE) {
            mContent.setVisibility(View.VISIBLE);
            mErrorLayout.setVisibility(View.GONE);
        }

        try {
            ClassificationPresenter.getInstance().categoryFirstList(new DataResponseListener<List<CategoryFirst>>() {
                @Override
                public void onSuccess(List<CategoryFirst> dataResponse) {
                    mList1.clear();
                    mList1.addAll(dataResponse);
                    if (mList1.size() > 0) {
                        mAdapter1.notifyDataSetChanged();
                        mSmallClassRV.setVisibility(View.VISIBLE);
                        getCategoryList2(mList1.get(0).getCate_id());
                    }
                }

                @Override
                public void onFail(String failMessage) {
                    toast(failMessage);
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                    mContent.setVisibility(View.GONE);
                    mErrorLayout.setVisibility(View.VISIBLE);
                }
            });
        } catch (Exception e) {
            mContent.setVisibility(View.GONE);
            mErrorLayout.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 获取首页二级分类列表
     */
    public void getCategoryList2(String cate_id) {
        if (mEmpty.getVisibility() == View.VISIBLE) {
            mEmpty.setVisibility(View.GONE);
        }
        if (mError.getVisibility() == View.VISIBLE) {
            mError.setVisibility(View.GONE);
        }

        if (mLoading2.getVisibility() == View.GONE) {
            mLoading2.setVisibility(View.VISIBLE);
        }
        mList2.clear();
        mAdapter2.notifyDataSetChanged();
        try {
            ClassificationPresenter.getInstance().categorySecondList(cate_id, new DataResponseListener<List<CategoryFirst>>() {
                @Override
                public void onSuccess(List<CategoryFirst> dataResponse) {
                    mLoading2.setVisibility(View.GONE);

                    mList2.clear();
                    if (dataResponse != null) {
                        for (CategoryFirst category : dataResponse) {
                            category.setSecond(true);
                            mList2.add(category);
/*
                            if (category.getSon() != null)
                                mList2.addAll(category.getSon());*/
                        }
                    }

                    if (mList2.size() > 0) {
                        mEmpty.setVisibility(View.GONE);
                        mError.setVisibility(View.GONE);
                        mAdapter2.notifyDataSetChanged();
                    } else {
                        mEmpty.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onFail(String failMessage) {
                    mLoading2.setVisibility(View.GONE);
                    mEmpty.setVisibility(View.VISIBLE);

                    toast(failMessage);
                }

                @Override
                public void onError(Throwable e) {
                    mLoading2.setVisibility(View.GONE);
                    mError.setVisibility(View.VISIBLE);
                    toast(R.string.network_or_server_exception);
                }
            });
        } catch (Exception e) {
            Log.e("TAG", e.toString());
            toast(getString(R.string.data_parse_fail));
        }
    }
}
